/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DeleteBook.java
 *
 * Created on Sep 16, 2010, 3:20:14 PM
 */

package COMIC_BOOK_STORE;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


/**
 *
 * @author izzah
 */
    public class BOOK_DELETE extends javax.swing.JFrame {

        Connection con;
        Statement stat;
         ResultSet rset;

    /** Creates new form DeleteBook */
    public BOOK_DELETE() throws SQLException {
         super("DELETE BOOK");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        choiceTextField = new javax.swing.JTextField();
        displaybookTitle = new javax.swing.JLabel();
        bookTitleLabel = new javax.swing.JLabel();
        volumeLabel = new javax.swing.JLabel();
        displayAuthor = new javax.swing.JLabel();
        authorLabel = new javax.swing.JLabel();
        publisherLabel = new javax.swing.JLabel();
        displayPublisher = new javax.swing.JLabel();
        displayVolume = new javax.swing.JLabel();
        genreLabel = new javax.swing.JLabel();
        displayGenre = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        searchButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        Lookuplabel = new javax.swing.JLabel();
        LookupComboBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        choiceTextField.setBounds(370, 50, 180, 40);
        jLayeredPane1.add(choiceTextField, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displaybookTitle.setText("x");
        displaybookTitle.setBounds(200, 160, 190, 30);
        jLayeredPane1.add(displaybookTitle, javax.swing.JLayeredPane.DEFAULT_LAYER);

        bookTitleLabel.setText("Book Title ");
        bookTitleLabel.setBounds(70, 164, 100, 20);
        jLayeredPane1.add(bookTitleLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        volumeLabel.setText("Volume ");
        volumeLabel.setBounds(70, 290, 90, 14);
        jLayeredPane1.add(volumeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayAuthor.setText("x");
        displayAuthor.setBounds(200, 240, 210, 30);
        jLayeredPane1.add(displayAuthor, javax.swing.JLayeredPane.DEFAULT_LAYER);

        authorLabel.setText("Author ");
        authorLabel.setBounds(70, 250, 80, 14);
        jLayeredPane1.add(authorLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        publisherLabel.setText("Publisher ");
        publisherLabel.setBounds(70, 210, 90, 14);
        jLayeredPane1.add(publisherLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayPublisher.setText("x");
        displayPublisher.setBounds(200, 200, 210, 30);
        jLayeredPane1.add(displayPublisher, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayVolume.setText("x");
        displayVolume.setBounds(200, 280, 200, 30);
        jLayeredPane1.add(displayVolume, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genreLabel.setText("Genre ");
        genreLabel.setBounds(70, 330, 90, 14);
        jLayeredPane1.add(genreLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        displayGenre.setText("x");
        displayGenre.setBounds(200, 320, 210, 30);
        jLayeredPane1.add(displayGenre, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 132, 900, 10);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        searchButton.setText("SEARCH");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });
        searchButton.setBounds(600, 50, 130, 40);
        jLayeredPane1.add(searchButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        deleteButton.setText("DELETE");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        deleteButton.setBounds(480, 390, 120, 40);
        jLayeredPane1.add(deleteButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        cancelButton.setBounds(610, 390, 130, 40);
        jLayeredPane1.add(cancelButton, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Lookuplabel.setText("Lookup by");
        Lookuplabel.setBounds(70, 50, 130, 40);
        jLayeredPane1.add(Lookuplabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        LookupComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Book Code", "Book ISBN" }));
        LookupComboBox.setBounds(210, 50, 120, 40);
        jLayeredPane1.add(LookupComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMAGE_REPOSITORY/Background5 copy.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(-40, 0, 940, 500);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 900, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        try {
            new BOOK_RECORD().setVisible(true); // TODO add your handling code here:
        } catch (SQLException ex) {
            Logger.getLogger(BOOK_DELETE.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(false);
    }                                            

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

        String query = null ;
        String choice = choiceTextField.getText();
        String combo_select = (String) LookupComboBox.getSelectedItem();

        if(combo_select.equals("Select")) {
            JOptionPane.showMessageDialog(null, "Please select lookup by first!");
        }

        else if(combo_select.equals("Book Code")) {
            query = "SELECT BOOK_TITLE,BOOK_PUBLISHER,BOOK_AUTHOR,BOOK_VOLUME,BOOK_GENRE FROM BOOK WHERE BOOK_CODE ='"+ choice+"'";
                   
        }
        else if(combo_select.equals("Book ISBN")) {
            query = "SELECT BOOK_TITLE,BOOK_PUBLISHER,BOOK_AUTHOR,BOOK_VOLUME,BOOK_GENRE FROM BOOK WHERE BOOK_ISBN ='"+ choice+"'";
        }

            try {
                stat = con.createStatement();
                 rset = stat.executeQuery(query);

                  if(rset.next())
                  {
                displaybookTitle.setText(rset.getString("BOOK_TITLE"));
                displayPublisher.setText(rset.getString("BOOK_PUBLISHER"));
                displayAuthor.setText(rset.getString("BOOK_AUTHOR"));
                displayVolume.setText(rset.getString("BOOK_VOLUME"));
                displayGenre.setText(rset.getString("BOOK_GENRE"));


            }



            } catch (SQLException ex) {
                Logger.getLogger(BOOK_DELETE.class.getName()).log(Level.SEVERE, null, ex);
            }
        // TODO add your handling code here:
    }                                            

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

         String query = null;
        String choice = choiceTextField.getText();
        String combo_select = (String)LookupComboBox.getSelectedItem();

          if(choice.equals(""))
        {
            JOptionPane.showMessageDialog(null, "Nothing to delete!!");
        }

         else{

            if(combo_select.equals("Book Code")) {
            query = "DELETE FROM BOOK WHERE BOOK_CODE ='"+ choice +"'";
            }

            if(combo_select.equals("Book ISBN")) {
            query = "DELETE FROM BOOK WHERE BOOK_ISBN ='"+ choice +"'";
            }

            String message = "Are you sure to delete?";
            String title = "DELETE BOOK";

             int reply = JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_OPTION);

             if(reply == JOptionPane.YES_OPTION) {


                try {
                    stat = con.createStatement();
                     stat.executeUpdate(query);

                      JOptionPane.showMessageDialog(null, "Delete Successful");

                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_DELETE.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "Delete Failed!");
                }

            }

          clear();
 }

        // TODO add your handling code here:
    }                                            


     private void clear()
    {
                displaybookTitle.setText("");
                displayPublisher.setText("");
                displayAuthor.setText("");
                displayVolume.setText("");
                displayGenre.setText("");
                choiceTextField.setText("");
               
    }

     private void connect() throws SQLException
    {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        System.out.println("Driver Loaded");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","javaproject","javaproject");
        System.out.println("Database Connected");

    }

    /**
    * @param args the command line arguments
    */
       public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new BOOK_DELETE().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(BOOK_DELETE.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JComboBox LookupComboBox;
    private javax.swing.JLabel Lookuplabel;
    private javax.swing.JLabel authorLabel;
    private javax.swing.JLabel bookTitleLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField choiceTextField;
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel displayAuthor;
    private javax.swing.JLabel displayGenre;
    private javax.swing.JLabel displayPublisher;
    private javax.swing.JLabel displayVolume;
    private javax.swing.JLabel displaybookTitle;
    private javax.swing.JLabel genreLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel publisherLabel;
    private javax.swing.JButton searchButton;
    private javax.swing.JLabel volumeLabel;
    // End of variables declaration                   

}
